package www.core.com.filter;

import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.web.WebApplicationInitializer;
@HandlesTypes(WebApplicationInitializer.class)  
public class SpringServletContainerInitializer implements
		ServletContainerInitializer {

	public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
			throws ServletException {
		// TODO Auto-generated method stub
		   List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();  
		   
	        if (webAppInitializerClasses != null) {  
	            for (Class<?> waiClass : webAppInitializerClasses) {  
	                // Be defensive: Some servlet containers provide us with invalid classes,  
	                // no matter what @HandlesTypes says...  
	                if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&  
	                        WebApplicationInitializer.class.isAssignableFrom(waiClass)) {  
	                    try {  
	                        initializers.add((WebApplicationInitializer) waiClass.newInstance());  
	                    }  
	                    catch (Throwable ex) {  
	                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);  
	                    }  
	                }  
	            }  
	        }  
	  
	        if (initializers.isEmpty()) {  
	            servletContext.log("No Spring WebApplicationInitializer types detected on classpath");  
	            return;  
	        }  
	  
	        AnnotationAwareOrderComparator.sort(initializers);  
	        servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers);  
	  
	        for (WebApplicationInitializer initializer : initializers) {  
	            initializer.onStartup(servletContext);  
	        }  
	}

}
